home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6562 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: cscsun3.larc.nasa.gov!hook
  2. From: hook@cscsun3.larc.nasa.gov (Ed Hook)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: C beginner needs your help ASAP
  5. Date: 19 Feb 1996 15:09:12 GMT
  6. Organization: CSC/NASA Langley Research Center
  7. Distribution: world
  8. Message-ID: <4ga3qo$25l@reznor.larc.nasa.gov>
  9. References: <4g862f$p0b@risky.ecs.umass.edu> <Pine.A32.3.91.960218205210.105198F-100000@black.weeg.uiowa.edu>
  10. Reply-To: hook@cscsun3.larc.nasa.gov
  11. NNTP-Posting-Host: cscsun3.larc.nasa.gov
  12.  
  13. In article <Pine.A32.3.91.960218205210.105198F-100000@black.weeg.uiowa.edu>, The Amorphous Mass <robinson@blue.weeg.uiowa.edu> writes:
  14. |> On Sun, 18 Feb 1996 sebag@ecs.umass.edu wrote:
  15. |> 
  16. |> > I am a new C programmmer who desperately needs help.
  17. |> > I have been digging in the manuals for a way to do this but have still
  18. |> > come out empty handed. Here is the problem: I want to open files in a while
  19. |> > loop with different filenames. data0,data1,data2,data3, .. data1000 , ...
  20. |> > I need to increment an integer each time around then convert it to a string
  21. |> > and then somehow use strcat to combine the "data" with the integer string.
  22. |> > After that use fopen(filename, "a");
  23. |> 
  24. |>   Assuming proper #inclusions and variable declarations, etc.:
  25. |> 
  26. |>     for (i = 0; i != 1000; ++i) {
  27. |>         sprintf(filename, "data%d", i);
  28. |>     fopen(filename, "a");
  29.  
  30.    It might be more useful to code this as:
  31.  
  32.     if ((fp=fopen(filename,"a")) == NULL ) {
  33.         /* deal with error */
  34.     }
  35.  
  36.    since then there would be some way to _actually_ write to the
  37.    file. Also, since there's never been any mention of closing any
  38.    of these files, 'fopen()' _will_ eventually fail, so the error
  39.    checking is not simply _pro_forma_
  40.  
  41. |>         /* ... */
  42. |>     }
  43. |> 
  44. |>   will open files data0 ... data999.  sprintf() is a wonderful thing. :)
  45. |> 
  46. |>   Posted and emailed.
  47. |> 
  48. |> 
  49. |> - James                    "Good candidates for exceptions are situations
  50. |> james-robinson@uiowa.edu   that do not occur under normal circumstances."
  51. |>                                                         -- Kevin J. Hopps
  52.  
  53. -- 
  54.  Ed Hook                              |       Coppula eam, se non posit
  55.  Computer Sciences Corporation        |         acceptera jocularum.
  56.  NASA Langley Research Center         | Me? Speak for my employer?...<*snort*>
  57.  Internet: hook@cscsun3.larc.nasa.gov |        ... Get a _clue_ !!! ...
  58.